package com.tydic.common.core.utils.file;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Slf4j
public class PdfUtils {

    public static void convertToImages(File pdfFile, File imageFile) throws IOException {
        FileUtil.mkParentDirs(imageFile);
        PDDocument pdDocument = PDDocument.load(pdfFile);
        PDFRenderer renderer = new PDFRenderer(pdDocument);
        int pages = pdDocument.getNumberOfPages();
        BufferedImage[] bufferedImages = new BufferedImage[pages];
        for (int i = 0; i < pages; i++) {
            BufferedImage image = renderer.renderImageWithDPI(i, 480);
            bufferedImages[i] = image;
        }
        ImageUtils.mergeImage(bufferedImages, 2, imageFile);
    }

    public static byte[] convertToImages(InputStream pdfFile) throws IOException {
        PDDocument pdDocument = PDDocument.load(pdfFile);
        PDFRenderer renderer = new PDFRenderer(pdDocument);
        int pages = pdDocument.getNumberOfPages();
        BufferedImage[] bufferedImages = new BufferedImage[pages];
        for (int i = 0; i < pages; i++) {
            BufferedImage image = renderer.renderImageWithDPI(i, 480);
            bufferedImages[i] = image;
        }
        return ImageUtils.mergeImage(bufferedImages, 2);
    }

    public static byte[] convertToImages(String url) {
        HttpResponse response = null;
        InputStream inputStream = null;
        try {
            response = HttpRequest.get(url).execute();
            if (response.isOk()) {
                inputStream = response.bodyStream();
                return convertToImages(inputStream);
            }
        } catch (Exception e) {
            log.error("预览pdf失败", e);
        } finally {
            IoUtil.close(inputStream);
            IoUtil.close(response);
        }
        return new byte[0];
    }

    public static byte[] toBytes(String url) {
        HttpResponse response = null;
        InputStream inputStream = null;
        try {
            response = HttpRequest.get(url).execute();
            if (response.isOk()) {
                return response.bodyBytes();
            }
        } catch (Exception e) {
            log.error("预览pdf失败", e);
        } finally {
            IoUtil.close(inputStream);
            IoUtil.close(response);
        }
        return new byte[0];
    }

    public static void previewPdf(String url, HttpServletResponse httpServletResponse) {
        HttpResponse response = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            response = HttpRequest.get(url).execute();
            if (response.isOk()) {
                inputStream = response.bodyStream();
                byte[] pngImages = convertToImages(inputStream);
                outputStream = httpServletResponse.getOutputStream();
                httpServletResponse.setCharacterEncoding("utf-8");
                httpServletResponse.setContentType("image/png");
                outputStream.write(pngImages, 0, pngImages.length);
                outputStream.flush();
            }
        } catch (Exception e) {
            log.error("预览pdf失败", e);
        } finally {
            IoUtil.close(outputStream);
            IoUtil.close(inputStream);
            IoUtil.close(response);
        }
    }
}
